﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public static class PlayerInventory
{
    public static List<CraftMaterialAmount> inventory = new List<CraftMaterialAmount>();
    public static List<Outfit> ownedOutfits = new List<Outfit>();
    public static List<Weapon> ownedWeapons = new List<Weapon>();

    public static Outfit equippedOutfit;
    public static Weapon equippedWeapon;

    static PlayerInventory()
    {
        if (PlayerPrefs.HasKey("equippedOutfit"))
        {
            equippedOutfit = SingletonManager.FindOutfit(PlayerPrefs.GetString("equippedOutfit"));
        }

        if (PlayerPrefs.HasKey("equippedWeapon"))
        {
            equippedWeapon = SingletonManager.FindWeapon(PlayerPrefs.GetString("equippedWeapon"));
        }

        foreach (Outfit outfit in SingletonManager.getOutfits())
        {
            if (outfit.byDefault)
            {
                ownedOutfits.Add(outfit);
                if (equippedOutfit == null) equippedOutfit = outfit;
            }
            if (PlayerPrefs.HasKey(outfit.key + "_owned"))
            {
                ownedOutfits.Add(outfit);
            }
        }

        foreach (Weapon weapon in SingletonManager.getWeapons())
        {
            if (weapon.byDefault)
            {
                ownedWeapons.Add(weapon);
                if (equippedWeapon == null) equippedWeapon = weapon;
            }
            if (PlayerPrefs.HasKey(weapon.key + "_owned"))
            {
                ownedWeapons.Add(weapon);
            }
        }

        foreach(CraftMaterial cm in SingletonManager.getMaterials())
        {
            if (PlayerPrefs.HasKey(cm.key + "_owned"))
            {
                inventory.Add(new CraftMaterialAmount(cm, PlayerPrefs.GetInt(cm.key + "_owned")));
            }
        }
    }

    public static void Add(CraftMaterial p_material, int p_amount)
    {
        CheckAndAdd(new CraftMaterialAmount(p_material, p_amount));
    }

    public static void Add(CraftMaterialAmount cma)
    {
        CheckAndAdd(cma);
    }

    public static void Add(List<CraftMaterialAmount> cmaList)
    {
        foreach (CraftMaterialAmount cma in cmaList)
        {
            CheckAndAdd(cma);
        }
    }

    public static bool Contains(CraftMaterial p_material)
    {
        foreach (CraftMaterialAmount cma in inventory)
        {
            if (cma.material.key == p_material.key) return true;
        }
        return false;
    }

    public static bool ContainsAmount(CraftMaterial p_material, int amount)
    {
        foreach (CraftMaterialAmount cma in inventory)
        {
            if (cma.material.key == p_material.key)
            {
                if (cma.amount >= amount) return true;
            }
        }
        return false;
    }

    public static bool CheckRecipe(Recipe r)
    {
        for (int i = 0; i < r.inputKeys.Length; i++)
        {
            if (!ContainsAmount(SingletonManager.FindMaterial(r.inputKeys[i]), r.inputAmounts[i])) return false;
        }
        return true;
    }

    private static void CheckAndAdd(CraftMaterialAmount cma)
    {
        bool found = false;
        foreach (CraftMaterialAmount invItem in inventory)
        {
            if (cma.material.key == invItem.material.key)
            {
                invItem.amount += cma.amount;
                found = true;
            }
        }
        if (!found)
        {
            inventory.Add(cma);
        }
        if (PlayerPrefs.HasKey(cma.material.key + "_owned"))
        {
            PlayerPrefs.SetInt(cma.material.key + "_owned", PlayerPrefs.GetInt(cma.material.key + "_owned") + cma.amount);
        }
        else
        {
            PlayerPrefs.SetInt(cma.material.key + "_owned", cma.amount);
        }
    }

    private static bool Remove(CraftMaterialAmount cma)
    {
        foreach (CraftMaterialAmount invItem in inventory)
        {
            if (cma.material.key == invItem.material.key && invItem.amount - cma.amount >= 0)
            {
                invItem.amount -= cma.amount;
                PlayerPrefs.SetInt(cma.material.key + "_owned", PlayerPrefs.GetInt(cma.material.key + "_owned") - cma.amount);
                return true;
            }
        }
        return false;
    }

    public static bool RunRecipe(Recipe r)
    {
        if (CheckRecipe(r))
        {
            for (int i = 0; i < r.inputKeys.Length; i++)
            {
                Remove(new CraftMaterialAmount(SingletonManager.FindMaterial(r.inputKeys[i]), r.inputAmounts[i]));
            }
            IRecipeOutput output = SingletonManager.Find(r.output);
            if (output.key.StartsWith("outfit"))
            {
                ownedOutfits.Add((Outfit)output);
                PlayerPrefs.SetInt(output.key + "_owned", 1);
            }
            else if (output.key.StartsWith("weapon"))
            {
                ownedWeapons.Add((Weapon)output);
                PlayerPrefs.SetInt(output.key + "_owned", 1);
            }
            else if (output.key.StartsWith("material")) Add((CraftMaterial)output, r.outputAmt);
            return true;
        }
        return false;
    }
}
